﻿using GameCreator.Core;
using Sirenix.OdinInspector;
using UnityEngine;
using Application = UnityEngine.Device.Application;

namespace Gp.Scripts.Core
{
    public class EntityBehaviour : GlobalID, IGUID
    {
        [HideLabel]
        [SerializeField] private EntityConfigInfo entityConfig;
        
        public BaseEntity Entity { get; private set; }
        [LabelText("单位阵营")]
        public UnitCampNumber campNumber = UnitCampNumber.MIDDLE; 

        public void SetEditorConfig(EntityConfigInfo config)
        {
            entityConfig = config;
        }


        public void Initialize()
        {
            var data = GetConfig();
            Entity = UnitEntityFactory.CreateUnit(data);
            Entity.EntityBehaviour = this;
            Entity.Initialize(data);
            Global.SendEvent(new Event_Entity.Spawn(Entity));
        }
        
        private void OnDestroy()
        {
            if (!Application.isPlaying || Entity == null) return;
            Global.SendEvent(new Event_Entity.Destroy(Entity));
            Entity.Destroy();
        }

        public Unit.ConfigUnit GetConfig()
        {
            return entityConfig.GetUnitConfig();
        }

        public string Guid => GetID();
        
        
        
        #if UNITY_EDITOR
        [Button("载入模型", ButtonSizes.Large)]
        public void EditorLoadModel()
        {
            if (!this.entityConfig.ValidateInfo())
            {
                UnityEditor.EditorWindow.focusedWindow.ShowNotification(new GUIContent("角色信息不完善！"));
                return;
            }
            Helpers.ObjectHelper.LoadModel(this);
        }

        [Button("同步名称")]
        public void SyncGameObjectName()
        {
            name = entityConfig.name;
        }
        #endif
    }
}